package com.haruhi.controller;

import com.haruhi.entity.FileDetail;
import com.haruhi.entity.vo.ResponseEntity;
import com.haruhi.service.FileDetailService;
import com.haruhi.utils.ParamUtil;
import com.haruhi.utils.RedisUtil;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ManageController {

    @Value("${manage.username}")
    private String username;

    @Value("${manage.password}")
    private String password;


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private FileDetailService fileDetailService;

    @PostMapping("/admin_login")
    public ResponseEntity login(@RequestBody Map<String,String> body){
        String name = null;
        String pwd = null;
        try {
            name = body.get("username");
            pwd = body.get("password");
        }catch (NullPointerException e){
            e.printStackTrace();
            return ResponseEntity.fail(-3,"param error");
        }
        if(ParamUtil.isEmpty(name) || ParamUtil.isEmpty(pwd)){
            return ResponseEntity.fail(-3,"param error");
        }
        if(!username.equals(name) || !password.equals(DigestUtils.md5DigestAsHex(pwd.getBytes()))){
            return ResponseEntity.fail(-2,"login fail");
        }
        return ResponseEntity.success(0,"OK",username);
    }

    /**
     * 返回令牌和过期时间
     * @return
     */
    @PostMapping("/m/getToken")
    public ResponseEntity getToken(){
        long timeout = redisUtil.getExpire("token");
        if(-2 == timeout){
            return ResponseEntity.fail(-3,"token不存在");
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("timeout",timeout);
        result.put("token",redisUtil.get("token").toString());
        return ResponseEntity.success("OK",result);

    }

    /**
     * 设置令牌/刷新
     * 若原本就已经存在令牌,再设置则是刷新
     * @param body
     * @return
     */
    @PostMapping("/m/setToken")
    public ResponseEntity setToken(@RequestBody HashMap<String,String> body){
//        String key = null;
        Long time = null;
        String val = null;
        try {
//            key = body.get("key");
            time = Long.parseLong(body.get("time"));
            val = body.get("value");
        }catch (NullPointerException e){
            e.printStackTrace();
            return ResponseEntity.fail(-3,"param error");
        }catch (NumberFormatException e){
            e.printStackTrace();
            return ResponseEntity.fail(-3,"param error");
        }
//        System.out.println(time);
        if(ParamUtil.isEmpty(time) || ParamUtil.isEmpty(val)){
            return ResponseEntity.fail(-3,"param error");
        }
        redisUtil.set("token",val,time);
        return ResponseEntity.success("OK");
    }

    @GetMapping("/m/all")
    public ResponseEntity queryAll(){
        return ResponseEntity.success(fileDetailService.getByCondition(new FileDetail()));
    }

    @PostMapping("/m/delete")
    public ResponseEntity deleteFile(@RequestBody FileDetail detail){
        if(detail == null || ParamUtil.isEmpty(detail.getId()) || ParamUtil.isEmpty(detail.getMd5())){
            return ResponseEntity.fail(-2,"参数错误");
        }

        fileDetailService.deleteFile(detail);
        return ResponseEntity.success("删除成功");
    }
}
